package com.mason.forum.controller;

import com.mason.forum.common.APPResult;
import com.mason.forum.common.ResultCode;
import com.mason.forum.model.Board;
import com.mason.forum.service.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-06
 * Time: 15:50
 */

@Slf4j
@Api(tags = "板块相关接口")
@RestController
@RequestMapping("/board")
public class BoardController {

    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;
    //注入配置中设置的板块数，默认值就是冒号后面的9
    // 为系统设置，不是传值设置

    @Resource
    private IBoardService boardService;

    /**
     * 查询板块列表
     * @return
     */
    @ApiOperation("获取板块列表")
    @GetMapping("/list")
    public APPResult<List<Board>> topList() {
        log.info("首页板块个数为：" + indexBoardNum);
        // 调用service层
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards == null) {
            boards = new ArrayList<>();
        }
        // 返回数据
        return APPResult.success(boards);
    }

    /**
     * 通过id查询板块
     * @param boardId 板块id
     * @return 板块信息
     */
    @ApiOperation("获取板块列表")
    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public APPResult<Board> selectById(@ApiParam("板块id") @RequestParam("boardId") @NonNull Long boardId) {
        // service - 查询板块信息
        Board board = boardService.selectById(boardId);
        if(board == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            return APPResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        // 成功返回
        return APPResult.success(board);
    }

}
